import {toPlainObjectDeep} from '../../../utils/toPlainObjectDeep';
import {observable, action} from 'mobx';
import {AppBean, AppSearchBean, AppController} from '../../../model/app/App';
import {AdAppPositionController} from '../../../model/ad/adPosition/AdAppPositionController'
import _ from 'lodash';
import {FormStore, ListStore, FieldItem, isRequired, isSelectRequired, toJSDeep} from '../../common/store/Store';
import {message} from 'antd';

const appController = new AppController();
const adAppPositionController = new AdAppPositionController();

export class AppFields implements AppBean<FieldItem> {
    constructor(type = 'add'){
        if(type === 'edit'){
            _.forEach(this, (field:FieldItem, key: string) => {
                if(field.hasFeedback === true){
                    field.validateStatus = 'success';
                }
            })  
        }
    }

    id: FieldItem = new FieldItem({
        value: null
    });
    description: FieldItem = new FieldItem({
        value: ''
    });
    name: FieldItem = new FieldItem({
        value: '',
        hasFeedback: true,
        required: true
    });
    title: FieldItem = new FieldItem({
        value: '',
        hasFeedback: true,
        required: true
    });
    buId: FieldItem = new FieldItem({
        value: '-1',
        hasFeedback: true,
        required: true,
        disabled: false,
        option:[{
            id: -1,
            name: '请选择',
            title:'请选择',
            description:'请选择'
        }]
    });
}

export class AppSearchFields implements AppSearchBean<FieldItem>{
    description: FieldItem = new FieldItem({
        value: ''
    });
    title: FieldItem = new FieldItem({
        value: ''
    });
}

const addDefaultFields = toPlainObjectDeep(new AppFields());
const editDefaultFields = toPlainObjectDeep(new AppFields('edit'));

export class AppBaseFormStore extends FormStore<AppFields, AppBean<any>> {
    @observable confirmLoading:boolean = false;

    @action changeConfirmLoading(params){
        this.confirmLoading = params;
    }

    validators = {
        title: [isRequired],
        name: [isRequired],
        buId: [isSelectRequired]
    };

    /*==================级联菜单开始==========================*/
    @action getBu(partnerId = null){
        return adAppPositionController
            .getPartnerBu({partnerId: partnerId})
            .then((data)=>{
                this.fields.buId.option = _.cloneDeep(toJSDeep(this.fields.buId.option)).concat(data.result);
                return data;
            });
    }

    @action clearBu(){
        this.fields.buId = _.cloneDeep(this.defaultFields.buId);
    }

    @action clearSelectAll(){
        this.clearBu();
    }

    @action selectBu(){
        return new Promise((get) => {
            get(this.fields.buId.value);
        })
    }

    @action bindData(){
        this.changeFields({
            buId: { disabled:true },
            title: { disabled:true },
            name: { disabled:true }
        });
        this.getBu();
    }

    /*==================级联菜单结束==========================*/
}

export class Add extends AppBaseFormStore {
    defaultFields = _.cloneDeep(addDefaultFields);

    @observable fields: AppFields = _.cloneDeep(addDefaultFields);

    @action load(){
        this.getBu();
        this.show();
    }

    @action addApp(params:AppBean<any>){
        return appController.addApp(params);
    }
}

export class Edit extends AppBaseFormStore {
    defaultFields = _.cloneDeep(editDefaultFields);

    @observable fields: AppFields = _.cloneDeep(editDefaultFields);

    @action load(id: number) {
        appController.getApp({id:id}).then((res) => {
            if(res.success){
                let data = res.result;
                this.bindData();
                this.show(data);
            }else{
                message.error(res.message,3);
            }
        })
    }

    @action editApp(params:AppBean<any>){
        return appController.editApp(params);
    }
}

class Check extends AppBaseFormStore {
    @observable fields: AppFields = _.cloneDeep(addDefaultFields);
}

export class List extends ListStore<AppBean<any>> {
    @action searchApp(params?:any){
        this._SpiLoading = true;

        appController.searchApp(params).then((data) =>{
            this.searchCallback(data);
        })
    }

    @action delete(id: number){
       return appController.deleteApp({id:id});
    }

    @action load(params:any = {}){
        this.searchApp(params);
    }

    @action refresh(search: Search){
        this.changeState(true);
        search.clearAll();
        this.load();
    }
}

const searchDefaultFields = toPlainObjectDeep(new AppSearchFields());

class Search extends FormStore<AppSearchFields,AppSearchBean<any>>{
    
    defaultFields = _.cloneDeep(searchDefaultFields);

    @observable fields: AppSearchFields = _.cloneDeep(searchDefaultFields);

    @observable expand:boolean = false;

    @action OnExpand(expand){
        this.expand = !expand;
    }

    @action clearSearch(list:List){
        this.clearAll();
        list.load();
    }

    @action clearSearchState(){
        this.expand = false;
        this.clearAll();
    }
}

export const listStore = new List();
export const addStore = new Add();
export const editStore = new Edit();
export const checkStore = new Check();
export const searchStore = new Search();
